﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.MODEL.PrintManage;
using MINERP.MODEL.FoundationModel;
using MINERP.BLL.PrintManage;
using MINERP.COM;
using MINERP.BLL.Common;

using FastReport;

namespace MINERP.Xray
{
    public partial class FrmChangePaper : Form
    {
        private PaperBLL paperBLL = new PaperBLL();
        private double paperTitle = 0;//纸头
        //private double paperEnd;//纸尾
        private Printer_Info printerInfo = null;

        /// <summary>
        /// 纸头
        /// </summary>
        public double PaperTitle
        {
            set { paperTitle = value; }
        }
        /// <summary>
        /// 纸尾
        /// </summary>
        //public double PaperEnd
        //{
        //    set{paperEnd =value;}
        //}
        /// <summary>
        /// 打印机
        /// </summary>
        public Printer_Info PrinterInfo
        {
            set { printerInfo = value; }
        }

        public delegate void ChangePaperCompleteHander();
        public event ChangePaperCompleteHander ChangePaperCompleteEvent;

        public FrmChangePaper()
        {
            InitializeComponent();
            txtPrintPaperId.Focus();
        }

        private void txtPrintPaperId_Enter(object sender, EventArgs e)
        {
            btnOk.PerformClick();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            int paperWasteRecordId = 0;
            PaperInfoModel prePaperInfo = null;
            if (string.IsNullOrEmpty(txtPrintPaperId.Text.Trim()))
            {
                MessageBox.Show("请输入打印纸条码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPrintPaperId.Focus();
                return;
            }

            PaperInfoModel paperInfo = paperBLL.GetPaper(Convert.ToInt32(txtPrintPaperId.Text));

            if (paperInfo != null)
            {
                if (paperInfo.Status != (int)PaperInfoStatus.Using)
                {
                    if (paperInfo.Status == (int)PaperInfoStatus.Complete)
                    {
                        MessageBox.Show("该打印纸已标志完成！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        //SetPaperTitle();
                        //paperInfo.LastLength -= paperTitle;
                        if (paperInfo.LastLength * 100 > 10)
                        {
                            int ret = paperBLL.ChangePaper(paperInfo, printerInfo, paperTitle, out paperWasteRecordId, out prePaperInfo);

                            if (ret == 0)
                            {
                                ((FrmMain)this.Owner).CurPaperInfoModel = paperInfo;
                                ChangePaperCompleteEvent();
                                //ShowBarcodePrintReview(paperWasteRecordId);

                                if (prePaperInfo != null)
                                {
                                    if (DialogResult.OK == MessageBox.Show("上一卷纸条码为：" + prePaperInfo.PrintPaperId + ",剩余长度为：" + (prePaperInfo.LastLength * 100).ToString() + "厘米，  是否标志完成？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                                    {
                                        FrmPaperEnd frmPaperEnd = new FrmPaperEnd();
                                        frmPaperEnd.PaperInfo = prePaperInfo;
                                        if (DialogResult.OK == frmPaperEnd.ShowDialog())
                                        {
                                            prePaperInfo.Status = (int)PaperInfoStatus.Complete;
                                            if (prePaperInfo.LastLength < 0)//这种情况纸尾就不是浪费的
                                            {
                                                prePaperInfo.Length += frmPaperEnd.PaperEndLength;
                                            }
                                            else
                                            {                                               
                                                prePaperInfo.Length = prePaperInfo.Length - prePaperInfo.LastLength + frmPaperEnd.PaperEndLength;
                                                prePaperInfo.LastLength = frmPaperEnd.PaperEndLength;

                                                if (frmPaperEnd.PaperEndLength > 0)
                                                {
                                                    //记录纸尾
                                                    int pwasteId = 0;
                                                    PaperWasteRecord pwasteRecord = new PaperWasteRecord();
                                                    PaperWasteTypeBLL pwasteType = new PaperWasteTypeBLL();
                                                    PaperWasteRecordModel pwInfo = new PaperWasteRecordModel();
                                                    pwInfo.CreateTime = UnityMethod.GetSystemTime();
                                                    pwInfo.Length = prePaperInfo.LastLength;
                                                    pwInfo.PaperInfo = prePaperInfo;
                                                    pwInfo.PaperWasteType = pwasteType.GetPaperWasteTypeInfo("纸尾");
                                                    pwInfo.Status = (int)WasterPaperStatus.unSure;
                                                    pwInfo.Width = prePaperInfo.Width;
                                                    pwInfo.PrinterWaste = true;
                                                    int inret = pwasteRecord.Insert(pwInfo, out pwasteId);
                                                    if (inret == 0)
                                                    {
                                                        ShowBarcodePrintReview(pwasteId);
                                                    }
                                                    else
                                                    {
                                                        WriteLogForCheck.WriteLogFile("记录纸尾失败" + DateTime.Now.ToString());
                                                    }
                                                }
                                            }
                                            paperBLL.Update(prePaperInfo);
                                        }
                                    }
                                }
                                MessageBox.Show("换纸成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                this.Close();
                            }
                            else
                            {
                                MessageBox.Show("换纸失败，请检查网络连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }


                        }
                        else
                        {
                            MessageBox.Show("该打印纸长度不够！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }

                }
                else
                {
                    MessageBox.Show("该打印纸正在使用中！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
            else
            {
                MessageBox.Show("没有找到该打印纸！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        /// <summary>
        /// 设置纸头长度
        /// </summary>
        private void SetPaperTitle()
        {
            FrmPaperTitle frmPaperTitle = new FrmPaperTitle();
            frmPaperTitle.ShowDialog(this);
        }
        /// <summary>
        /// 设置纸尾长度
        /// </summary>
        private void SetPaperEnd(PaperInfoModel paperInfo)
        {
            FrmPaperEnd frmPaperEnd = new FrmPaperEnd();
            frmPaperEnd.PaperInfo = paperInfo;
            frmPaperEnd.ShowDialog(this);
        }

        private void FrmChangePaper_Load(object sender, EventArgs e)
        {

        }
        private void FrmChangePaper_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                lbPaperBarcode.Text = string.Empty;
                lbPaperFace.Text = string.Empty;
                lbPaperLength.Text = string.Empty;
                lbPaperStatus.Text = string.Empty;
                lbPaperType.Text = string.Empty;
                lbPaperWidth.Text = string.Empty;

                PaperInfoModel paperInfo = paperBLL.GetPaper(Convert.ToInt32(txtPrintPaperId.Text));
                if (paperInfo != null)
                {
                    lbPaperBarcode.Text = paperInfo.PrintPaperId.ToString();
                    lbPaperFace.Text = paperInfo.PaperFaceName;
                    lbPaperLength.Text = paperInfo.LastLength.ToString() + "米";
                    lbPaperStatus.Text = paperInfo.StatusName;
                    lbPaperType.Text = paperInfo.PaperTypeName;
                    lbPaperWidth.Text = (paperInfo.Width * 100 / 2.54).ToString() + "英寸";
                }
                else
                {
                    MessageBox.Show("没有找到该打印纸！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 显示废纸条码打印预览
        /// </summary>
        /// <param name="paperWasteRecordId"></param>
        private void ShowBarcodePrintReview(int paperWasteRecordId)
        {
            FrmPrintBarcode frmBarcode = new FrmPrintBarcode();
            frmBarcode.BarCode = paperWasteRecordId;
            frmBarcode.ShowDialog();
        }

    }
}
